package com.szh.mysobproject.ui.activity

import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import by.kirich1409.viewbindingdelegate.activityViewBinding
import by.kirich1409.viewbindingdelegate.viewBinding
import com.szh.mysobproject.R
import com.szh.mysobproject.app.AppActivity
import com.szh.mysobproject.databinding.CreationCenterActivityBinding
import com.szh.mysobproject.manager.UserManager
import com.szh.mysobproject.other.GridSpaceDecoration
import com.szh.mysobproject.ui.adapter.AchievementAdapter
import com.szh.mysobproject.util.*
import com.szh.mysobproject.viewmodel.UserViewModel

/**
 * @author : create by  szh
 * @date : 2022/7/13 9:57
 * @desc :创作中心activity
 */

class CreationCenterActivity : AppActivity() {

    private val mBinding by viewBinding<CreationCenterActivityBinding>()
    private val mUserViewModel by viewModels<UserViewModel>()
    private val mAchievementAdapter = AchievementAdapter()

    override fun getLayoutId(): Int = R.layout.creation_center_activity

    override fun initView() {
        mBinding.rvAchievementList.apply {
            layoutManager = GridLayoutManager(context, 2)
            adapter = mAchievementAdapter
            addItemDecoration(GridSpaceDecoration(4.dp))
        }
    }

    override fun initData() {
        val data = arrayListOf(Pair(0, 0), Pair(0, 0), Pair(0, 0), Pair(0, 0))
        mAchievementAdapter.setData(data)
    }

    override fun initEvent() {
        mBinding.llUserInfoContainer.setFixOnClickListener {
            takeIfLogin { userBasicInfo ->
                val userId = userBasicInfo.id
                ViewUserActivity.start(this, userId)
            }
        }
    }

    override fun initObserver() {
        checkToken {
            val userBasicInfo = it.getOrNull()
            mBinding.imageAvatar.loadAvatar(UserManager.currUserIsVip(), userBasicInfo?.avatar)
            mBinding.tvNickName.text = userBasicInfo?.nickname ?: "账号未登录"
        }
        mUserViewModel.getAchievement().observe(this) {
            val userAchievement = it.getOrNull() ?: return@observe
            val data = arrayListOf<Pair<Int, Int>>().apply {
                add(Pair(userAchievement.atotalView, userAchievement.articleDxView))
                add(Pair(userAchievement.thumbUpTotal, userAchievement.thumbUpDx))
                add(Pair(userAchievement.fansCount, userAchievement.fansDx))
                add(Pair(userAchievement.sob, userAchievement.sobDx))
            }
            mAchievementAdapter.setData(data)
        }
    }

    override fun isStatusBarDarkFont(): Boolean = true
}